package c.c.b.b.f;

import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.text.TextUtils;
import c.c.b.b.e.f;
import c.c.b.b.i.e;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/* compiled from: Request */
public abstract class c<T> implements Comparable<c<T>> {
    public static final int METHOD_DELETE = 3;
    public static final int METHOD_DEPRECATED_GET_OR_POST = -1;
    public static final int METHOD_GET = 0;
    public static final int METHOD_HEAD = 4;
    public static final int METHOD_OPTIONS = 5;
    public static final int METHOD_PATCH = 7;
    public static final int METHOD_POST = 1;
    public static final int METHOD_PUT = 2;
    public static final int METHOD_TRACE = 6;
    public c.c.b.b.f.p.a<T> a;
    public Handler b;
    /* access modifiers changed from: private|final */
    public final c.c.b.b.f.q.a c;
    private final int d;
    private String e;
    private String f;
    private final int g;
    private final Object h;
    private Integer i;
    private o j;
    private boolean k;
    private boolean l;
    private boolean m;
    private boolean n;
    private e o;
    private c.c.b.b.i.b.a p;
    private Object q;
    private long r;
    private long s;
    private boolean t;
    private String u;
    private Map<String, Object> v;
    private b w;

    /* compiled from: Request */
    public class a implements Runnable {
        public final /* synthetic */ String a;
        public final /* synthetic */ long b;

        public a(String str, long j) {
            this.a = str;
            this.b = j;
        }

        public void run() {
            c.this.c.b(this.a, this.b);
            c.this.c.a(c.this.toString());
        }
    }

    /* compiled from: Request */
    public interface b {
    }

    /* compiled from: Request */
    public enum c {
        LOW,
        NORMAL,
        HIGH,
        IMMEDIATE
    }

    @Deprecated
    public c(String str, c.c.b.b.f.p.a aVar) {
        this(-1, str, aVar);
    }

    private static int b(String str) {
        try {
            if (!TextUtils.isEmpty(str)) {
                Uri parse = Uri.parse(str);
                if (parse != null) {
                    str = parse.getHost();
                    if (str != null) {
                        return str.hashCode();
                    }
                }
            }
        } catch (Throwable unused) {
        }
        return 0;
    }

    public abstract p<T> a(m mVar);

    public c.c.b.b.h.a a(c.c.b.b.h.a aVar) {
        return aVar;
    }

    public void a(long j, long j2) {
    }

    public abstract void a(p<T> pVar);

    public c addExtra(String str, Object obj) {
        if (!(TextUtils.isEmpty(str) || obj == null)) {
            if (this.v == null) {
                this.v = new HashMap();
            }
            this.v.put(str, obj);
        }
        return this;
    }

    public void addMarker(String str) {
        if (c.c.b.b.f.q.a.c) {
            this.c.b(str, Thread.currentThread().getId());
        }
    }

    public void build(o oVar) {
        if (oVar != null) {
            oVar.a(this);
        }
    }

    public Map<String, String> c() throws c.c.b.b.h.b {
        return null;
    }

    public void cancel() {
        synchronized (this.h) {
            this.l = true;
            this.a = null;
        }
    }

    public String d() {
        return "UTF-8";
    }

    public void deliverError(p<T> pVar) {
        c.c.b.b.f.p.a aVar;
        synchronized (this.h) {
            aVar = this.a;
        }
        if (aVar != null) {
            aVar.b(pVar);
        }
    }

    public void e() {
        b bVar;
        synchronized (this.h) {
            bVar = this.w;
        }
        if (bVar != null) {
            ((c.c.b.b.f.h.a) bVar).a(this);
        }
    }

    public c.c.b.b.f.p.a getBaseListener() {
        c.c.b.b.f.p.a aVar;
        synchronized (this.h) {
            aVar = this.a;
        }
        return aVar;
    }

    public byte[] getBody() throws c.c.b.b.h.b {
        Map c = c();
        return (c == null || c.size() <= 0) ? null : a(c, d());
    }

    public String getBodyContentType() {
        StringBuilder g = c.b.a.a.a.g("application/x-www-form-urlencoded; charset=");
        g.append(d());
        return g.toString();
    }

    public c.c.b.b.i.b.a getCacheEntry() {
        return this.p;
    }

    public String getCacheKey() {
        String url = getUrl();
        int method = getMethod();
        if (method == 0 || method == -1) {
            return url;
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(Integer.toString(method));
        stringBuilder.append('-');
        stringBuilder.append(url);
        return stringBuilder.toString();
    }

    public Map<String, Object> getExtra() {
        return this.v;
    }

    public Map<String, String> getHeaders() throws c.c.b.b.h.b {
        return Collections.emptyMap();
    }

    public String getIpAddrStr() {
        return this.u;
    }

    public int getMethod() {
        return this.d;
    }

    public long getNetDuration() {
        return this.s;
    }

    @Deprecated
    public byte[] getPostBody() throws c.c.b.b.h.b {
        Map a = a();
        return (a == null || a.size() <= 0) ? null : a(a, b());
    }

    public c getPriority() {
        return c.NORMAL;
    }

    public final o getRequestQueue() {
        return this.j;
    }

    public e getRetryPolicy() {
        return this.o;
    }

    public final int getSequence() {
        Integer num = this.i;
        if (num != null) {
            return num.intValue();
        }
        throw new IllegalStateException("getSequence called before setSequence");
    }

    public long getStartTime() {
        return this.r;
    }

    public Object getTag() {
        return this.q;
    }

    public final int getTimeoutMs() {
        return ((i) getRetryPolicy()).a;
    }

    public int getTrafficStatsTag() {
        return this.g;
    }

    public String getUrl() {
        return this.e;
    }

    public String getUserAgent() {
        return this.f;
    }

    public boolean hasHadResponseDelivered() {
        boolean z;
        synchronized (this.h) {
            z = this.m;
        }
        return z;
    }

    public boolean isCanceled() {
        boolean z;
        synchronized (this.h) {
            z = this.l;
        }
        return z;
    }

    public boolean isResponseOnMain() {
        return this.t;
    }

    public void markDelivered() {
        synchronized (this.h) {
            this.m = true;
        }
    }

    public c<?> setCacheEntry(c.c.b.b.i.b.a aVar) {
        this.p = aVar;
        return this;
    }

    public void setIpAddrStr(String str) {
        this.u = str;
    }

    public void setNetDuration(long j) {
        this.s = j;
    }

    public c<?> setRequestQueue(o oVar) {
        this.j = oVar;
        return this;
    }

    public c<?> setResponseOnMain(boolean z) {
        this.t = z;
        return this;
    }

    public c<?> setRetryPolicy(e eVar) {
        this.o = eVar;
        return this;
    }

    public final c<?> setSequence(int i) {
        this.i = Integer.valueOf(i);
        return this;
    }

    public final c<?> setShouldCache(boolean z) {
        this.k = z;
        return this;
    }

    public final c<?> setShouldRetryServerErrors(boolean z) {
        this.n = z;
        return this;
    }

    public void setStartTime() {
        this.r = SystemClock.elapsedRealtime();
    }

    public c<?> setTag(Object obj) {
        this.q = obj;
        return this;
    }

    public void setUrl(String str) {
        this.e = str;
    }

    public c<?> setUserAgent(String str) {
        this.f = str;
        return this;
    }

    public final boolean shouldCache() {
        return this.k;
    }

    public final boolean shouldRetryServerErrors() {
        return this.n;
    }

    public String toString() {
        StringBuilder g = c.b.a.a.a.g("0x");
        g.append(Integer.toHexString(getTrafficStatsTag()));
        String stringBuilder = g.toString();
        StringBuilder stringBuilder2 = new StringBuilder();
        stringBuilder2.append(isCanceled() ? "[X] " : "[ ] ");
        stringBuilder2.append(getUrl());
        String str = " ";
        stringBuilder2.append(str);
        stringBuilder2.append(stringBuilder);
        stringBuilder2.append(str);
        stringBuilder2.append(getPriority());
        stringBuilder2.append(str);
        stringBuilder2.append(this.i);
        return stringBuilder2.toString();
    }

    public c(int i, String str, c.c.b.b.f.p.a aVar) {
        this.c = c.c.b.b.f.q.a.c ? new c.c.b.b.f.q.a() : null;
        this.f = "VADNetAgent/0";
        this.h = new Object();
        this.k = true;
        this.l = false;
        this.m = false;
        this.n = false;
        this.p = null;
        this.r = 0;
        this.s = 0;
        this.t = true;
        this.b = new Handler(Looper.getMainLooper());
        this.d = i;
        this.e = str;
        this.a = aVar;
        setRetryPolicy(new i());
        this.g = b(str);
    }

    public void a(String str) {
        o oVar = this.j;
        if (oVar != null) {
            synchronized (oVar.b) {
                oVar.b.remove(this);
            }
            synchronized (oVar.j) {
                for (c.c.b.b.f.o.b a : oVar.j) {
                    a.a(this);
                }
            }
            oVar.b(this, 5);
        }
        if (c.c.b.b.f.q.a.c) {
            long id = Thread.currentThread().getId();
            if (Looper.myLooper() != Looper.getMainLooper()) {
                this.b.post(new a(str, id));
            } else {
                this.c.b(str, id);
                this.c.a(toString());
            }
        }
    }

    public int compareTo(c<T> cVar) {
        c priority = getPriority();
        c priority2 = cVar.getPriority();
        return priority == priority2 ? this.i.intValue() - cVar.i.intValue() : priority2.ordinal() - priority.ordinal();
    }

    @Deprecated
    public String b() {
        return d();
    }

    public void b(p<?> pVar) {
        b bVar;
        synchronized (this.h) {
            bVar = this.w;
        }
        if (bVar != null) {
            c.c.b.b.f.h.a aVar = (c.c.b.b.f.h.a) bVar;
            c.c.b.b.i.b.a aVar2 = pVar.b;
            if (aVar2 != null) {
                if ((aVar2.f < System.currentTimeMillis() ? 1 : null) == null) {
                    List<c> list;
                    String cacheKey = getCacheKey();
                    synchronized (aVar) {
                        list = (List) aVar.a.remove(cacheKey);
                    }
                    if (list != null) {
                        if (q.a) {
                            q.a("Releasing %d waiting requests for cacheKey=%s.", Integer.valueOf(list.size()), cacheKey);
                        }
                        for (c cVar : list) {
                            k kVar = (k) aVar.b.d;
                            kVar.a(cVar, pVar, null);
                            c.c.b.b.e.c cVar2 = kVar.c;
                            if (cVar2 != null) {
                                ((f) cVar2).c(cVar, pVar);
                            }
                        }
                        return;
                    }
                    return;
                }
            }
            aVar.a(this);
        }
    }

    public void a(int i) {
        o oVar = this.j;
        if (oVar != null) {
            oVar.b(this, i);
        }
    }

    @Deprecated
    public Map<String, String> a() throws c.c.b.b.h.b {
        return c();
    }

    private byte[] a(Map<String, String> map, String str) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            for (Entry entry : map.entrySet()) {
                if (entry.getKey() == null || entry.getValue() == null) {
                    throw new IllegalArgumentException(String.format("Request#getParams() or Request#getPostParams() returned a map containing a null key or value: (%s, %s). All keys and values must be non-null.", new Object[]{entry.getKey(), entry.getValue()}));
                }
                stringBuilder.append(URLEncoder.encode((String) entry.getKey(), str));
                stringBuilder.append('=');
                stringBuilder.append(URLEncoder.encode((String) entry.getValue(), str));
                stringBuilder.append('&');
            }
            return stringBuilder.toString().getBytes(str);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(c.b.a.a.a.D("Encoding not supported: ", str), e);
        }
    }

    public void a(b bVar) {
        synchronized (this.h) {
            this.w = bVar;
        }
    }
}
